<?php
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// DLMan by Shedd Technologies International		  				//
// http://www.dlman.com | info@dlman.com							//
// Copyright 2003 by STI, All rights reserved.						//
// ---------------------------------------------------------------- //
// Usage of this software is governed by the terms of GPL. 	    	//
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

//////////////////////////////////////////////////////////////////////
//  FUNCTIONS
//////////////////////////////////////////////////////////////////////

function sheader(){
	global $config;
	ob_start();
	?>
		<html>
		<head>
			<style type="text/css">
				<?php
				/*
					$sql="SELECT * FROM ".$config->dt[templates]." WHERE name='mastercss';";
					$result=mysql_query($sql);
					$value=@mysql_fetch_array($result);
					$data = addslashes($value['template']);
				*/
				$data = addslashes(template("mastercss"));
				eval("echo stripslashes(\"$data\");");
				?>
			</style>
			<script type="text/javascript">
			<!--
			function toggleBox(szDivID, iState) // 1 visible, 0 hidden
			{		
				if(document.layers)	   //NN4+
			    {
			       document.layers[szDivID].visibility = iState ? "show" : "hide";
			    }
			    else if(document.getElementById)	  //gecko(NN6) + IE 5+
			    {
			        var obj = document.getElementById(szDivID);
			        obj.style.visibility = iState ? "visible" : "hidden";
			    }
			    else if(document.all)	// IE 4
			    {
			        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
			    }
			}
			// -->
			</script>
			<title><?php print $config->cs['title']; ?></title>
		</head>
		<?php
			/*
			$sql="SELECT * FROM ".$config->dt[templates]." WHERE name='bodyinsert';";
			$result=mysql_query($sql);
			$value=@mysql_fetch_array($result);
			$data = addslashes($value['template']);
			*/
			$data = addslashes(template("bodyinsert"));
		?>
		<body <?php eval("echo stripslashes(\"$data\");"); ?>>
	<?php
	$final=ob_get_contents();
	ob_end_clean();
	return $final;
}//end function header

//--------------------------------------------------------------------

function footer(){
	ob_start();
	?>
		</body>
		</html>
	<?php
	$final=ob_get_contents();
	ob_end_clean();
	return $final;
}//end function footer

//--------------------------------------------------------------------
/*
function login(){
	global $config;
	sheader();
	$sql="SELECT * FROM ".$config->dt[templates]." WHERE name='login';";
	$result=mysql_query($sql);
	$value=mysql_fetch_array($result);
	//login template is stored in database
	$data = addslashes($value['template']);
	eval("echo stripslashes(\"$data\");");
	footer();
	exit();
}//end login function
*/
//--------------------------------------------------------------------
/*
	Authenticate a user using the UIS standards.
*/
function authenticate($HTTP_COOKIE_VARS,$HTTP_POST_VARS){
	global $config;
	error_reporting (E_ERROR | E_PARSE);
	
	$user = ($HTTP_COOKIE_VARS['user']) ? $HTTP_COOKIE_VARS['user'] : $HTTP_POST_VARS['user'];
	$pass = ($HTTP_COOKIE_VARS['pass']) ? $HTTP_COOKIE_VARS['pass'] : $HTTP_POST_VARS['pass'];
	
	$sql="SELECT ".$config->field['userid'].",".$config->field['username'].",".$config->field['password']." FROM ".$config->dt['user']." WHERE ".$config->field['username']."='$user'";
	$r=mysql_query($sql);
	//md5 auth varient
	if($config->password=="md5"){
		$tp=md5($pass);
	}
	//vb3.0.0 md5_salt varient
	elseif($config->password=="md5_salt"){
		$sql="SELECT ".$config->field['salt']." FROM ".$config->dt['user']." WHERE ".$config->field['username']."='$user'";
		$pepper=mysql_query($sql);
		$salt=@mysql_fetch_array($pepper);
		$tp=md5(md5($pass).$salt['salt']);
		//die("salt<br>".$tp);
	}
		
	while ($row = @mysql_fetch_array($r)){
		if(strtolower($user) == strtolower($row[$config->field['username']]) && $tp == $row[$config->field['password']]){
			//setcookie("user","$user",time() + (9999 * 86400));
			//setcookie("pass",md5($pass),time() + (9999 * 86400));
			//setcookie("action","load",time() + (9999 * 86400));
			
			return true;
			exit();
		}
		else{
			return false;
			exit();
		}
	}//end while
	
}//end function authenticate

//--------------------------------------------------------------------
/*
	Verify a user's authentication - does not adjust cookies. (bool)
	$pass must be the md5 encrypted value of the real password to match
*/
function verify($user,$pass){
	global $config;
	
	//vb3.0.0 md5_salt varient
	if($config->password=="md5_salt"){
		$sql="SELECT ".$config->field['salt']." FROM ".$config->dt['user']." WHERE ".$config->field['username']."='$user'";
		$pepper=mysql_query($sql);
		$salt=@mysql_fetch_array($pepper);
		$tp=md5($pass.$salt['salt']);
		//die("salt<br>".$tp);
	}
	
	$sql="SELECT ".$config->field['username'].",".$config->field['password']." FROM ".$config->dt['user']." WHERE ".$config->field['username']."='$user'";
	$r=mysql_query($sql);
	while ($row = @mysql_fetch_array($r)){
	    if(strtolower($user) == strtolower($row[$config->field['username']]) && $tp == $row[$config->field['password']]){
	        return true;
	        exit();
	    }
		else{
			return false;
			exit();
		}
	}//end while
}//end function verify

//--------------------------------------------------------------------

function iff($arg, $res){
	if($arg){
		return $res;
	}
}

//--------------------------------------------------------------------

/*
PHP Builder Function
http://www.phpbuilder.com/columns/akent20000610.php3?page=6

Interval can be one of: 
yyyy     year
q	Quarter
m	Month
y	Day of year
d	Day
w	Weekday
ww  Week of year
h	Hour
n	Minute
s	Second
*/
function DateAdd($interval,  $number, $date) {
    $date_time_array  = getdate($date);
    
	$hours =  $date_time_array["hours"];
	$minutes =  $date_time_array["minutes"];
	$seconds =  $date_time_array["seconds"];
	$month =  $date_time_array["mon"];
	$day =  $date_time_array["mday"];
	$year =  $date_time_array["year"];

    switch ($interval) {
    
        case "yyyy":
            $year +=$number;
            break;        
        case "q":
            $year +=($number*3);
            break;        
        case "m":
            $month +=$number;
            break;        
        case "y":
        case "d":
        case "w":
             $day+=$number;
            break;        
        case "ww":
             $day+=($number*7);
            break;        
        case "h":
             $hours+=$number;
            break;        
        case "n":
             $minutes+=$number;
            break;        
        case "s":
             $seconds+=$number;
            break;        

    }//edn switch
	$timestamp =  mktime($hours ,$minutes, $seconds,$month ,$day, $year);
    return $timestamp;
}

//--------------------------------------------------------------------

/*
PHP Builder Function
http://www.phpbuilder.com/columns/akent20000610.php3?page=7

interval: "w", "d", "h", "n" or "s"
*/
function DateDiff ($interval, $date1,$date2) {
    // get the number of seconds between the two dates
	$timedifference =  $date2 - $date1;
    
    switch ($interval) {
        case "w":
            $retval  = bcdiv($timedifference ,604800);
            break;
        case "d":
            $retval  = bcdiv( $timedifference,86400);
            break;
        case "h":
             $retval = bcdiv ($timedifference,3600);
            break;        
        case "n":
            $retval  = bcdiv( $timedifference,60);
            break;        
        case "s":
            $retval  = $timedifference;
            break;        
    }
    return $retval;
}

//--------------------------------------------------------------------

function make_password($length){
    $vowels = 'aeiouyAEIOUY';
    $consonants = 'bdghjlmnpqrstvwxzBDGHJLMNPQRSTVWXZ';
    $password = '';
    
    $alt = time() % 2;
    srand(time());

    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % 17)];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % 6)];
            $alt = 1;
        }
    }
    return $password;
}

//--------------------------------------------------------------------

//return template data
function template($name){
	global $config;
	$sql="SELECT * FROM ".$config->dt['templates']." WHERE name='".$name."';";
	$result=mysql_query($sql);
	$value=mysql_fetch_array($result);
	return $value['template'];
}

//--------------------------------------------------------------------

//Function to seed random numbers with microseconds
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}

//--------------------------------------------------------------------

//redirection function
function redirect($url){
	//header("location: $url");
	?>
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
		<html>
		<head>
			<title>Redirection...</title>
			<meta http-equiv="REFRESH" content="0; url=<?php print $url; ?>">
		</head>
		<body>
			<P align=center><STRONG>Redirecting...</STRONG></P>
			<P align=center><a href="<?php print $url; ?>">Click here if your browser does not automatically forward you.</a></P>
		</body>
		</html>
	<?php
}

//--------------------------------------------------------------------
?>
